package com.web.farme.redis;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DefautlRedisCache implements RedisCache {

    private final RedisSerializer redisSerializer;

    private final RedisTemplate<String, String> redisTemplate;

    @Override
    public boolean delete(String key) {
        Boolean result = this.redisTemplate.delete(key);
        return result != null && result;
    }

    @Override
    public boolean deletes(Collection<String> keys) {
        Long delete = this.redisTemplate.delete(keys);
        return delete != null && delete != 0;
    }

    @Override
    public boolean containsKey(String key) {
        Boolean result = this.redisTemplate.hasKey(key);
        return result != null && result;
    }

    @Override
    public <T> boolean set(String key, T item) {
        this.redisTemplate.opsForValue().set(key, this.redisSerializer.serialize(item));
        return true;
    }

    @Override
    public <T> boolean set(String key, T item, long time, TimeUnit var) {
        this.redisTemplate.opsForValue().set(key, this.redisSerializer.serialize(item), time, var);
        return false;
    }

    @Override
    public <T> T get(String key, Class<T> var) {
        return this.redisSerializer.deSerialize(this.redisTemplate.opsForValue().get(key), var);
    }

    @Override
    public String get(String key) {
        return this.redisTemplate.opsForValue().get(key);
    }

    @Override
    public <T> boolean setMap(String key, String fieldName, T var3) {
        this.redisTemplate.opsForHash().put(key, fieldName, this.redisSerializer.serialize(var3));
        return true;
    }

    /**
     * 获取hashmap值
     */
    @Override
    public <T> T getMap(String var1, String var2, Class<T> var3) {
        return this.redisSerializer.deSerialize((String) this.redisTemplate.opsForHash().get(var1, var2), var3);
    }

    @Override
    public String getMap(String key, String var2) {
        return (String) this.redisTemplate.opsForHash().get(key,var2);
    }

    @Override
    public boolean deleteMap(String key, String fieldName) {
        return this.redisTemplate.opsForHash().delete(key, fieldName) > 0L;
    }

    @Override
    public boolean deleteMap(String key, Collection<String> fieldNames) {
        return this.redisTemplate.opsForHash().delete(key, fieldNames.toArray()) > 0L;
    }

    /**
     * 这个是获取键值对
     */
    @Override
    public Map<String, String> getAllMap(String key) {
        Map<String, String> result = new HashMap<>();
        for (Map.Entry<Object, Object> entry : this.redisTemplate.opsForHash().entries(key).entrySet()) {
            result.put((String) entry.getKey(), (String) entry.getValue());
        }
        return result;
    }

    @Override
    public <T> boolean hmSet(String key, Map<String, T> map) {
        this.redisTemplate.opsForHash().putAll(key, map);
        return true;
    }

    @Override
    public List<String> hmGet(String key, Collection<String> fieldNames) {
        return this.redisTemplate.opsForHash().multiGet(key, Arrays.asList(fieldNames.toArray())).stream().map(v -> (String) v).collect(Collectors.toList());
    }

    @Override
    public <T> List<T> hmGet(String key, Collection<String> fieldNames, Class<T> tClass) {
        return this.redisTemplate.opsForHash().multiGet(key, Arrays.asList(fieldNames.toArray())).stream().map(v -> this.redisSerializer.deSerialize((String) v, tClass)).collect(Collectors.toList());
    }


    /**
     * 设置过期时间
     */
    @Override
    public boolean expire(String key, long seconds) {
        Boolean expire = this.redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
        return expire != null && expire;
    }


}
